package com.example.leetcode;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TreeNumber {
    public static String ser(TreeNone treeNone){
        if(treeNone==null){
            return "{}";
        }
        //定义数的数组
        List<TreeNone> list= new ArrayList<>();
        list.add(treeNone);
        //小于就不要size-1
        for (int i=0;i<list.size();i++){
            TreeNone treeNone1= list.get(i);
            if (treeNone1==null){
                continue;
            }
            list.add(treeNone1.left);
            list.add(treeNone1.right);
        }
        while (list.get(list.size()-1)==null){
            list.remove(list.size()-1);
        }
        StringBuffer stringBuffer = new StringBuffer("{");
        stringBuffer.append(list.get(0).val);
        for (int i=1;i<list.size()-1;i++){
            if (list.get(i)==null){
                stringBuffer.append(",#");
            }
            stringBuffer.append(","+list.get(i).val);
        }
        stringBuffer.append("}");
        return stringBuffer.toString();

    }

   public TreeNone deser(String s){
        String[] arr= s.substring(0,s.length()-1).split(",");
        List<TreeNone> quese= new ArrayList<>();
        //初始化树，设值树
        TreeNone treeNone = new TreeNone(Integer.parseInt(arr[0]));
       quese.add(treeNone);
        int index=0;
        boolean left=true;
        for (int i=0;i<arr.length;i++){

            if (!"#" .equals(arr[i]) ){
//                树的赋值
                TreeNone treeNone1 = new TreeNone(Integer.parseInt(arr[i]));
                if (left){
                    //添加到左子树
                    quese.get(index).left=treeNone1;
                }else {
                    quese.get(index).right=treeNone1;
                }
                //添加进去
                quese.add(treeNone1);
            }
          if (!left){
              index++;
          }
          left=!left;
        }
        return quese.get(0);
    }
  public static void main(String[] args) {
      String s ="1,2,3,4";
    // String a= s.substring(1,s.length()-1);
      String[] arr= s.substring(0,s.length()-1).split(",");
      System.out.println(Arrays.toString(arr));
  }
}
